﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogisticsBLL;
using System.Data.SqlClient;
using System.Data;

namespace WebLosgic.Driver
{
    public partial class DriverBindTruck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //int DriverID = Convert.ToInt32(Request.QueryString["did"]);
                if (Request.QueryString["did"] != null)
                {
                    getBInd();
                    LabName.Text = Request.QueryString["name"].ToString();
                }
                else
                {
                    Server.Transfer("DriverXinxi.aspx");
                }
                
            }
        }

        public void getBInd()
        {
            int DriverID = Convert.ToInt32(Request.QueryString["did"]);
            DriverBLL dbl = new DriverBLL();
            int pageIndex = AspNetPagerTool.CurrentPageIndex;
            int pageSize = AspNetPagerTool.PageSize;
            int Count = 0;
            DataTable read = dbl.bindturkc(DriverID,pageIndex,pageSize,out Count);
            if (read.Rows.Count>0)
            {
                RptBindDrive.DataSource = read;
                AspNetPagerTool.RecordCount = Count;
                RptBindDrive.DataBind();
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('没有空闲车辆')", true);
            }
            
        }

        protected void RptBindDrive_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName== "binddate")
            {
                int DriverID = Convert.ToInt32(Request.QueryString["did"]);
                int Truckid = Convert.ToInt32(e.CommandArgument);
                DriverBLL drv = new DriverBLL();
                string str = drv.bindtruckdriver(Truckid, DriverID);

                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 绑定车辆驾驶员";
                int TypeId = 21;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + DriverID + "," + Truckid ;
                string ProcName = "insert into Contact values(@fktruckID,@fkdriverID)";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('" + str + "');location.href='DriverXinxi.aspx';", true);
                }
                getBInd();


            }
        }

        protected void BtnRrturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("DriverXinxi.aspx");
        }

        protected void AspNetPagerTool_PageChanged(object sender, EventArgs e)
        {
            getBInd();
        }
    }
}